package com.meila.cms.controller;

import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.meila.cms.auth.AuthClass;
import com.meila.cms.dao.DictMapper;
import com.meila.cms.model.CommonModel;
import com.meila.cms.model.DictModel;
import com.meila.cms.model.SQLAdapterModel;
import com.meila.cms.util.StringUtil;
import com.meila.cms.util.json.JsonUtil1;

@Controller
@RequestMapping("/common")
@AuthClass("login")
public class CommonController extends BaseController {
	private DictMapper dictMapper;

	public DictMapper getDictMapper() {
		return dictMapper;
	}

	@Inject
	public void setDictMapper(DictMapper dictMapper) {
		this.dictMapper = dictMapper;
	}

	/**
	 * 参数 category,isquery
	 */
	@RequestMapping("/selectByComboCategory")
	@ResponseBody
	public List selectByComboCategory(DictModel dictModel) {

		List<CommonModel> list = null;
		try {

			SQLAdapterModel sqlAdapterModel = new SQLAdapterModel(
					"select distinct type from t_dict where category ='"
							+ dictModel.getCategory() + "'");
			int type = dictMapper.selectCount(sqlAdapterModel);

			if (type == 1) {

				// 查询字典数据字典配置的sql
				sqlAdapterModel
						.setSql("select sqls from t_dict where category ='"
								+ dictModel.getCategory() + "'");
				String sqls = dictMapper.selectString(sqlAdapterModel);
				if("City".equals(dictModel.getCategory())){
					sqls=sqls.replace("${where}"," ParentId="+dictModel.getIdStr());
				}
				sqlAdapterModel.setSql(sqls);
				list = dictMapper.selectBySql(sqlAdapterModel);

			} else if (type == 0) {

				// 查询字典数据字典配置的是常量
				sqlAdapterModel
						.setSql("select name as text,value from t_dict where category ='"
								+ dictModel.getCategory() + "'");
				list = dictMapper.selectBySql(sqlAdapterModel);

			}

		} catch (Exception ex) {

			ex.printStackTrace();
			logger.error("出错:", ex);
		}
		return list;

	}

}
